package models

import (
	"github.com/jinzhu/gorm"
	"time"
)

// table users
type User struct {
	gorm.Model
	Email string `gorm:"unique_index;default:null"` //邮箱
	Telephone     string    `gorm:"unique_index;default:null"` //手机号码
	Password string `gorm:"default:null"` //密码
	VerifyState   string    `gorm:"default:'0'"`               //邮箱验证状态
	SecretKey     string    `gorm:"default:null"`              //密钥
	OutTime       time.Time //过期时间
	GithubLoginId string    `gorm:"unique_index;default:null"` // github唯一标识
	GithubUrl     string    //github地址
	IsAdmin   bool   //是否是管理员
	AvatarUrl string // 头像链接
	NickName      string    // 昵称
	LockState     bool      `gorm:"default:'0'"` //锁定状态
}

func GetUserByUsername(username string) (*User, error) {

	var user User
	err := DB.First(&user, "email = ?", username).Error
	return &user, err
}

//
func (user *User) Insert() error {
	return DB.Create(user).Error

}

func GetUser(id interface{}) (*User, error) {
	var user User
	err := DB.First(&user, id).Error
	return &user, err
}

func ListUsers() ([]*User, error) {
	var users []*User
	//err := DB.Find(&users, "is_admin = ?", false).Error
	err := DB.Find(&users).Error
	return users, err
}
